package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created by hanchao on 2018/1/12.
 */
@RestController
@RequestMapping(value = "/manage/user/")
public class UserManagerController {

    @Autowired
    private IUserService iUserService;

    @PostMapping(value = "login.do")
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> response =iUserService.login(username,password);
        if(response.isSuccess()){
            User user =response.getData();
            if(user.getRole() ==  Const.Role.ROLE_ADMIN){
                session.setAttribute(Const.CURRENT_USER,user);
                return response;
            }else{
                return ServerResponse.createdByErrorMessage("该用户不是管理员，无法登陆");
            }
        }
      return response;
    }



}
